package com.ly.heart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.core.domain.entity.SysDept;
import com.ly.common.exception.CustomException;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.StringUtils;
import com.ly.common.utils.erp.ConstUtil;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.KingOrgEntity;
import com.ly.heart.mapper.KingOrgMapper;
import com.ly.heart.service.IKingOrgService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.system.service.ISysDeptService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 绑定ERP组织和金蝶账套 服务实现类
 * </p>
 * @author WuZH
 * @since 2024-09-25
 */
@Service
public class KingOrgServiceImpl extends ServiceImpl<KingOrgMapper, KingOrgEntity> implements IKingOrgService {

    @Resource
    private ISysDeptService iSysDeptService;

    @Override
    public AjaxResult queryList (KingOrgEntity kingOrgEntity) {
        Page<KingOrgEntity> page = this.page ( new Page<> ( kingOrgEntity.getPage (), kingOrgEntity.getLimit () ), new LambdaQueryWrapper<KingOrgEntity> ().eq ( KingOrgEntity :: getIsUsed, ConstUtil.IS_USED_YES ) );
        page.getRecords ().forEach ( org -> {
            SysDept one = iSysDeptService.getOne ( new LambdaQueryWrapper<SysDept> ().eq ( SysDept :: getDeptId, org.getOrgId () ) );
            if ( one != null ) {
                org.setOrgName ( one.getDeptName () );
            }
        } );
        return AjaxResult.success ( page );
    }

    @Override
    public KingOrgEntity querySingle (String id) {
        KingOrgEntity orgEntity = this.getById ( id );
        SysDept one = iSysDeptService.getOne ( new LambdaQueryWrapper<SysDept> ().eq ( SysDept :: getDeptId, orgEntity.getOrgId () ) );
        if ( one != null ) {
            orgEntity.setOrgName ( one.getDeptName () );
        }
        return orgEntity;
    }

    @Override
    public AjaxResult saveSingle (KingOrgEntity kingOrgEntity) {
        if ( kingOrgEntity == null || StringUtils.isBlank ( kingOrgEntity.getOrgId () ) ) {
            return AjaxResult.error ( "新增对象不能为空" );
        }
        // 为了确保每个组织只有对应的一个金蝶账套，查询可用的数量
        Integer count = this.count ( new LambdaQueryWrapper<KingOrgEntity> ().
                eq ( KingOrgEntity :: getOrgId, kingOrgEntity.getOrgId () ).
                eq ( KingOrgEntity :: getIsDisabled, false ).
                eq ( KingOrgEntity :: getIsUsed, ConstUtil.IS_USED_YES ) );
        if ( count > 0 ) {
            return AjaxResult.error ( "每个组织同时只能绑定一个金蝶套账！" );
        }
        kingOrgEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        kingOrgEntity.setCreateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        kingOrgEntity.setCreateTime ( DateUtils.getNowDate () );
        boolean save = this.save ( kingOrgEntity );
        if ( ! save ) {
            throw new CustomException ( "新增ERP与金蝶套账绑定失败！" );
        }
        return AjaxResult.success ();
    }

    @Override
    public AjaxResult updateSingle (KingOrgEntity kingOrgEntity) {
        kingOrgEntity.setUpdateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        kingOrgEntity.setUpdateTime ( DateUtils.getNowDate () );
        boolean b = this.updateById ( kingOrgEntity );
        if ( ! b ) {
            throw new CustomException ( "修改ERP与金蝶套账绑定失败！" );
        }
        return AjaxResult.success ();
    }

    @Override
    public AjaxResult deleteSingle (String id) {
        boolean update = this.update ( new LambdaUpdateWrapper<KingOrgEntity> ().set ( KingOrgEntity :: getIsUsed, ConstUtil.IS_USED_NO ).eq ( KingOrgEntity :: getId, id ) );
        if ( ! update ) {
            throw new CustomException ( "删除ERP与金蝶套账绑定失败！" );
        }
        return AjaxResult.success ();
    }

    @Override
    public AjaxResult disabled (String id) {
        KingOrgEntity orgEntity = this.getById ( id );
        boolean update = this.update ( new LambdaUpdateWrapper<KingOrgEntity> ().
                set ( KingOrgEntity :: getIsDisabled, orgEntity.getIsDisabled () ? 0 : 1 ).
                eq ( KingOrgEntity :: getId, id ) );
        if ( ! update ) {
            throw new CustomException ( orgEntity.getIsDisabled () ? "禁用ERP与金蝶套账失败" : "解禁ERP与金蝶套账失败" );
        }
        return AjaxResult.success ( orgEntity.getIsDisabled () ? "禁用ERP与金蝶套账成功" : "解禁ERP与金蝶套账成功" );
    }
}
